var app = getApp();
let right = true;
let headimg = '';
let tid = 0;
let stype = 0;

//#region  接收消息格式转换

//接收消息格式转换
function formatGetMessage(data, headimgg = '') {
    //判断消息方向
    //如果自己是客服的情况下
    if (app.globalData.kefuInfo.id) {
        right = data.admin_user_id == app.globalData.kefuInfo.id ? true : false;
    } else {
        right = data.mid == app.globalData.memberinfo.id ? true : false;
    }

    console.log('formatGetMessage', data, right);
    //获取对方头像
    headimg = headimgg;
    if (data) {
        // "id": 115,
        // "aid": 3,
        // "mid": 0,//发送用户id
        // "admin_user_id": 2,//发送官方账号id 平台管理账号id
        // "to_mid": 393,//接收用户id
        // "to_admin_user_id": 0,//接收官方账号id 平台管理账号id
        // "type": 2,//类型 1=系统消息 2=聊天 3赞 4关注 5=评论
        // "sub_type": "",//模块类型 order=订单 money=余额
        // "msg_type": "txt",//消息体类型 txt=1 img=2 video=3
        // "title": "",//消息标题
        // "content": "🐖",//消息内容
        // "is_read": 0,//阅读状态 0未读 1已读
        // "path_type": 0,//跳转类型 1=小程序路径
        // "path": "",//跳转路径
        // "path_args": "",//自定义跳转路径参数
        // "createtime": "1744945630",
        // "uptime": ""
        switch (data.msg_type) {
            case 1:
                return gmsg_text(data);
            case 2:
                return gmsg_img(data);
            case 3:
                return gmsg_video(data);
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
            case 11:
            case 12:
            case 13:
            case 14:
            case 17:
            case 18:
                return gmsg_template(data);
            case 5:
                return gmsg_product(data);
            case 16:
                return gmsg_order(data);
            case 15:
                return gmsg_share(data);
        }
    }
}

function gmsg_text(data) {
    return {
        time: data.createtime,
        showTime: true,
        id: data.id,
        type: 'text', //消息类型
        right: right, //输出方
        content: data.content, //消息内容
        extend: null,
        loading: false,
        isRead: data.is_read == 1 ? true : false,
        avatar: right ? app.globalData.memberinfo.headimg : headimg,
    };
}

function gmsg_img(data) {
    return {
        time: data.createtime,
        showTime: true,
        id: data.id,
        type: 'image', //消息类型
        right: right, //输出方
        content: data.content, //消息内容
        extend: null,
        loading: false,
        isRead: data.is_read == 1 ? true : false,
        avatar: right ? app.globalData.memberinfo.headimg : headimg,
    };
}

function gmsg_video(data) {
    return {
        time: data.createtime,
        showTime: true,
        id: data.id,
        type: 'video', //消息类型
        right: right, //输出方
        content: data.content, //消息内容
        extend: {
            cover: data.data_args.video_image,
        },
        loading: false,
        isRead: data.is_read == 1 ? true : false,
        avatar: right ? app.globalData.memberinfo.headimg : headimg,
    };
}

function gmsg_template(data) {
    return {
        time: data.createtime,
        showTime: true,
        id: data.id,
        type: 'template',
        right: false,//模板消息一定是对方发送
        content: data.title,
        extend: {
            data_id: data.data_id,
            data_args: data.data_args,
            content: data.content,
            msg_type: data.msg_type,
        },
        loading: false,
        isRead: data.is_read == 1 ? true : false,
        avatar: right ? app.globalData.memberinfo.headimg : headimg,
    };
}

function gmsg_product(data) {
    return {
        time: data.createtime,
        showTime: true,
        id: data.id,
        type: 'product',
        right: right,
        content: data.data_args.name,
        extend: {
            pic: data.data_args.pic,
            sell_price: data.data_args.sell_price,
            proid: data.data_args.proid,
            sales: data.data_args.sales,
            ptype: productTypeCovert(data.data_args),
        },
        loading: false,
        isRead: data.is_read == 1 ? true : false,
        avatar: right ? app.globalData.memberinfo.headimg : headimg,
    };
}

function gmsg_share(data) {
    return {
        time: data.createtime,
        showTime: true,
        id: data.id,
        type: 'share',
        right: right,
        content: data.title,
        extend: {
            pic: data.content,
            shareid: data.data_id,
            admin_img: data.data_args.headimg,
            admin_name: data.data_args.nickname,
        },
        loading: false,
        isRead: data.is_read == 1 ? true : false,
        avatar: right ? app.globalData.memberinfo.headimg : headimg,
    }
}

function gmsg_order(data) {
    return {
        time: data.createtime,
        showTime: true,
        id: data.id,
        type: 'order',
        right: right,
        content: '',
        extend: {
            orderid: data.data_args.orderid,
            ordernum: data.data_args.ordernum,
            prolist: Object.prototype.toString.call(data.data_args.ogList) === '[object Object]' ? [data.data_args.ogList] : data.data_args.ogList,
            orderStatus: data.data_args.status_txt,
        },
        loading: false,
        isRead: data.is_read == 1 ? true : false,
        avatar: right ? app.globalData.memberinfo.headimg : headimg,
    };
}

function productTypeCovert(data_args) {
    let ptype = 'shop';
    if (data_args.seckill == 1) {
        ptype = 'seckill';
        return ptype
    }
    if (data_args.groupby == 1) {
        ptype = 'groupby';
        return ptype
    }
    return ptype
}
//#endregion

//#region  发送消息格式转换

//发送消息格式转换
function formatSendMessage(sendtype, id, type, content, extend = null) {
    console.log('formatSendMessage', sendtype, content);
    stype = Number(sendtype);
    tid = id;
    switch (type) {
        case 'text':
            return smsg_text(content, extend);
        case 'image':
            return smsg_img(content, extend);
        case 'video':
            return smsg_video(content, extend);
        case 'product':
            return smsg_product(content, extend);
        case 'order':
            return smsg_order(content, extend);
    }
}

function smsg_text(content, extend) {
    return {
        session_id: app.globalData.kefuInfo.id ? app.globalData.kefuInfo.id : null,
        content: content,
        type: 2,
        to_mid: stype != 1 ? tid : null,
        to_admin_user_id: stype == 1 ? tid : null,
        msg_type: 1,
        random_str: app.globalData.kefuInfo.random_str ? app.globalData.kefuInfo.random_str : null,
    };
}

function smsg_img(content, extend) {
    return {
        session_id: app.globalData.kefuInfo.id ? app.globalData.kefuInfo.id : null,
        content: content,
        type: 2,
        to_mid: stype != 1 ? tid : null,
        to_admin_user_id: stype == 1 ? tid : null,
        msg_type: 2,
        random_str: app.globalData.kefuInfo.random_str ? app.globalData.kefuInfo.random_str : null,
    };
}

function smsg_video(content, extend) {
    return {
        session_id: app.globalData.kefuInfo.id ? app.globalData.kefuInfo.id : null,
        content: content,
        type: 2,
        to_mid: stype != 1 ? tid : null,
        to_admin_user_id: stype == 1 ? tid : null,
        msg_type: 3,
        random_str: app.globalData.kefuInfo.random_str ? app.globalData.kefuInfo.random_str : null,
    };
}

function smsg_product(content, extend) {
    return {
        session_id: app.globalData.kefuInfo.id ? app.globalData.kefuInfo.id : null,
        type: 2,
        to_mid: stype != 1 ? tid : null,
        to_admin_user_id: stype == 1 ? tid : null,
        msg_type: 5,
        sub_type: 3,
        data_id: extend.proid,
        data_args: {
            seckill: extend.ptype == 'seckill' ? 1 : 0,  // 秒杀商品
            groupby: extend.ptype == 'groupby' ? 1 : 0,  // 团购商品
        },
        random_str: app.globalData.kefuInfo.random_str ? app.globalData.kefuInfo.random_str : null,
    };
}

function smsg_order(content, extend) {
    return {
        session_id: app.globalData.kefuInfo.id ? app.globalData.kefuInfo.id : null,
        type: 2,
        to_mid: stype != 1 ? tid : null,
        to_admin_user_id: stype == 1 ? tid : null,
        msg_type: 16,
        sub_type: 1,
        data_id: extend.orderid,
        random_str: app.globalData.kefuInfo.random_str ? app.globalData.kefuInfo.random_str : null,
    };
}

//#endregion

module.exports = {
    formatGetMessage: formatGetMessage,
    formatSendMessage: formatSendMessage
}